home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / archvrs / msdos / uudecode / uudecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  3.0 KB  |  198 lines

  1. /* uudecode.c */
  2.  
  3. #ifndef lint
  4. static char sccsid[] = "@(#)uudecode.c    5.1 (Berkeley) 7/2/83";
  5. #endif
  6.  
  7. /*
  8.  * uudecode [input]
  9.  *
  10.  * create the specified file, decoding as you go.
  11.  * used with uuencode.
  12.  */
  13. #include <stdio.h>
  14. #ifndef MSDOS
  15. #include <pwd.h>
  16. #endif
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19.  
  20. /* single character decode */
  21. #define DEC(c)    (((c) - ' ') & 077)
  22.  
  23. main(argc, argv)
  24. char **argv;
  25. {
  26.     FILE *in, *out;
  27.     struct stat sbuf;
  28.     int mode;
  29.     char dest[128];
  30.     char buf[80];
  31.  
  32.     /* optional input arg */
  33.     if (argc > 1) {
  34.         if ((in = fopen(argv[1], "r")) == NULL) {
  35.             perror(argv[1]);
  36.             exit(1);
  37.         }
  38.         argv++; argc--;
  39.     } else
  40.         in = stdin;
  41.  
  42.     if (argc != 1) {
  43.         printf("Usage: uudecode [infile]\n");
  44.         exit(2);
  45.     }
  46.  
  47.     /* search for header line */
  48.     for (;;) {
  49.         if (fgets(buf, sizeof buf, in) == NULL) {
  50.             fprintf(stderr, "No begin line\n");
  51.             exit(3);
  52.         }
  53.         if (strncmp(buf, "begin ", 6) == 0)
  54.             break;
  55.     }
  56.     sscanf(buf, "begin %o %s", &mode, dest);
  57.  
  58.     /* handle ~user/file format */
  59. #ifndef MSDOS
  60.     if (dest[0] == '~') {
  61.         char *sl;
  62.         struct passwd *getpwnam();
  63.         char *index();
  64.         struct passwd *user;
  65.         char dnbuf[100];
  66.  
  67.         sl = index(dest, '/');
  68.         if (sl == NULL) {
  69.             fprintf(stderr, "Illegal ~user\n");
  70.             exit(3);
  71.         }
  72.         *sl++ = 0;
  73.         user = getpwnam(dest+1);
  74.         if (user == NULL) {
  75.             fprintf(stderr, "No such user as %s\n", dest);
  76.             exit(4);
  77.         }
  78.         strcpy(dnbuf, user->pw_dir);
  79.         strcat(dnbuf, "/");
  80.         strcat(dnbuf, sl);
  81.         strcpy(dest, dnbuf);
  82.     }
  83. #endif
  84.  
  85.     /* create output file */
  86. #ifdef MSDOS
  87.     /* binary output file */
  88.     out = fopen(dest, "wb");
  89. #else
  90.     out = fopen(dest, "w");
  91. #endif
  92.     if (out == NULL) {
  93.         perror(dest);
  94.         exit(4);
  95.     }
  96.     chmod(dest, mode);
  97.  
  98.     decode(in, out);
  99.  
  100.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  101.         fprintf(stderr, "No end line\n");
  102.         exit(5);
  103.     }
  104.     exit(0);
  105. }
  106.  
  107. /*
  108.  * copy from in to out, decoding as you go along.
  109.  */
  110. decode(in, out)
  111. FILE *in;
  112. FILE *out;
  113. {
  114.     char buf[80];
  115.     char *bp;
  116.     int n;
  117.  
  118.     for (;;) {
  119.         /* for each input line */
  120.         if (fgets(buf, sizeof buf, in) == NULL) {
  121.             printf("Short file\n");
  122.             exit(10);
  123.         }
  124.         n = DEC(buf[0]);
  125.         if (n <= 0)
  126.             break;
  127.  
  128.         bp = &buf[1];
  129.         while (n > 0) {
  130.             outdec(bp, out, n);
  131.             bp += 4;
  132.             n -= 3;
  133.         }
  134.     }
  135. }
  136.  
  137. /*
  138.  * output a group of 3 bytes (4 input characters).
  139.  * the input chars are pointed to by p, they are to
  140.  * be output to file f.  n is used to tell us not to
  141.  * output all of them at the end of the file.
  142.  */
  143. outdec(p, f, n)
  144. char *p;
  145. FILE *f;
  146. {
  147.     int c1, c2, c3;
  148.  
  149.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  150.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  151.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  152.     if (n >= 1)
  153.         putc(c1, f);
  154.     if (n >= 2)
  155.         putc(c2, f);
  156.     if (n >= 3)
  157.         putc(c3, f);
  158. }
  159.  
  160.  
  161. /* fr: like read but stdio */
  162. int
  163. fr(fd, buf, cnt)
  164. FILE *fd;
  165. char *buf;
  166. int cnt;
  167. {
  168.     int c, i;
  169.  
  170.     for (i=0; i<cnt; i++) {
  171.         c = getc(fd);
  172.         if (c == EOF)
  173.             return(i);
  174.         buf[i] = c;
  175.     }
  176.     return (cnt);
  177. }
  178.  
  179. /*
  180.  * Return the ptr in sp at which the character c appears;
  181.  * NULL if not found
  182.  */
  183.  
  184. #define    NULL    0
  185.  
  186. char *
  187. index(sp, c)
  188. register char *sp, c;
  189. {
  190.     do {
  191.         if (*sp == c)
  192.             return(sp);
  193.     } while (*sp++);
  194.     return(NULL);
  195. }
  196.  
  197.  
  198.